package com.mj.service.system;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mj.dao.system.MenuMapper;
import com.mj.dao.system.RoleMapper;
import com.mj.model.system.Menu;
import com.mj.model.system.Role;
@Service("roleService")
public class RoleServiceImpl implements RoleService {
	@Resource
	private RoleMapper roleDao;
	@Resource
	private MenuMapper menuDao;
	@Override
	public Role selectById(Integer roleId) {
		return this.roleDao.selectByPrimaryKey(roleId);
	}
	@Override
	public List<Role> findAllRoleList(Map<String,Object> map) {
		return this.roleDao.findAllRole(map);
	}
	@Override
	public int addRole(Role record) {
		record.setAvailable(0);
		return this.roleDao.insert(record);
	}
	@Override
	public Role selectByRoleName(String uname) {
		return this.roleDao.selectByRoleName(uname);
	}
	@Override
	public Role selectByIdAndName(Integer uid, String uname) {
		return this.roleDao.selectByIdAndName(uid, uname);
	}
	@Override
	public int updateById(Role role) {
		return this.roleDao.updateByPrimaryKeySelective(role);
	}
	@Override
	public int deleteById(Integer id) {
		return this.roleDao.deleteByPrimaryKey(id);
	}
	@Override
	public List<Role> roleBindMenuName(String roleName) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("roleName", roleName);
		List<Role> rolelist = roleDao.findAllRole(map);
		for(Role r:rolelist){
			//菜单id
			String rids[] = r.getMenuIds().split(",");
			//根据菜单id查询相关名称
			List<Menu> mlist = menuDao.getAllMenuById(rids, "");
			String minfo = "";
			for(Menu m:mlist){
				minfo += m.getMenuName()+",";
			}
			//拥有菜单名称集合
			r.setMenuList(minfo);
		}
		return rolelist;
	}

}
